<template>
  <div class="app-container">
    <!--条件搜索表单-->
    <div style="margin-bottom: -20px">
      <el-form :inline="true" class="demo-form-inline">
        <el-form-item>
          <el-input v-model="searchObj.name" placeholder="负责人姓名" />
        </el-form-item>
        <el-form-item>
          <el-input v-model="searchObj.phone" placeholder="负责人电话" />
        </el-form-item>
        <el-button type="primary" icon="el-icon-search" @click="getAll()">查询</el-button>
        <el-button type="default" icon="el-icon-refresh" @click="reset()">清空</el-button>
      </el-form>
    </div>

    <!-- 工具栏 -->
    <div class="el-toolbar" v-if="roleId == 1">
      <div class="el-toolbar-body" style="justify-content: flex-start; margin-top: 15px">
        <el-button type="primary" icon="el-icon-plus" @click="openEdit(null)">新增</el-button>
        <el-button type="danger" icon="el-icon-delete" @click="batchDelete">批量删除</el-button>
      </div>
    </div>

    <!-- banner列表 :data="list"表示遍历list数组 stripe条纹 -->
    <el-table
      :data="data.list"
      :border="true"
      v-loading="loading"
      element-loading-text="正在努力加载数据中..."
      stripe
      style="width: 100%; margin: 15px 0px"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" fixed="left" width="55" />
      <el-table-column type="index" fixed="left" width="80" label="序号" />
      <el-table-column prop="labName" min-width="180" fixed="left" label="实验室名称" />
      <el-table-column prop="startTime" min-width="160" label="实验室开放时间" />
      <el-table-column prop="stopTime" min-width="160" label="实验室关闭时间" />
      <el-table-column prop="name" min-width="120" label="负责人姓名" />
      <el-table-column prop="sex" min-width="100" label="负责人性别" />
      <el-table-column prop="phone" min-width="120" label="负责人电话" />
      <el-table-column prop="createAt" label="创建人" />
      <el-table-column prop="createTime" min-width="160" label="创建时间" />
      <el-table-column prop="updateAt" min-width="100" label="最后修改人" />
      <el-table-column prop="updateTime" min-width="160" label="修改时间" />

      <!-- 操作 -->
      <el-table-column label="操作" v-if="roleId == 1" fixed="right" width="120" align="center">
        <template slot-scope="scope">
          <el-button type="danger" size="mini" icon="el-icon-delete" @click="remove(scope.row.id)"></el-button>
          <el-button type="primary" size="mini" icon="el-icon-edit" @click="openEdit(scope.row.id)"></el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 分页导航栏 -->
    <div>
      <el-pagination
        @current-change="getAll"
        @size-change="handleSizeChange"
        :current-page="pIndex"
        :page-size="pSize"
        :page-sizes="[3, 6, 9, 12, 15]"
        background
        layout="total, sizes, ->, prev, pager, next, jumper"
        :total="data.total"
      >
      </el-pagination>
    </div>
  </div>
</template>

<script>
//引入api接口定义的js文件
import openApi from '@/api/open'
export default {
  data() {
    // 定义变量和初始值
    return {
      pIndex: 1, // 当前页
      pSize: 6, // 每页显示记录数
      searchObj: {}, // 条件封装对象
      data: [], // 数据
      roleId: '',
      loading: false,
      ids: []
    }
  },
  created() {
    //勾子函数，在页面渲染之前执行，一般调用methods定义的方法得到数据
    this.getAll() //调用methods中的函数
    this.roleId = sessionStorage.getItem('roleId')
  },
  methods: {
    //实验室课程信息
    getAll(current = 1) {
      this.loading = true
      // 加载列表数据
      this.pIndex = current //添加当前页参数
      // axios异步获取远程数据（ajax），通过api
      openApi
        .getPage(this.pIndex, this.pSize, this.searchObj)
        .then((response) => {
          //请求成功response是接口返回数据
          //返回集合赋值list
          this.data = response.data
          this.loading = false
        })
        .catch((error) => {
          this.loading = false
          //请求失败
          console.log(error)
        })
    },

    // 改变页码
    handleSizeChange(pageSize) {
      this.pSize = pageSize
      //重新调用数据加载方法 --> 方法中已经加入的查询参数
      this.getAll()
    },

    // 复选框选中
    handleSelectionChange(r) {
      // 得到选中的id
      this.ids = r.map((v) => v.id)
    },

    // 打开表单
    openEdit(id) {
      const path = id != null ? `edit/${id}` : 'add'
      this.$router.push(`/open/${path}`)
    },

    //删除实验室开放信息
    remove(id) {
      this.$confirm('此操作将永久删除实验室开放信息, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        //确定执行then方法
        //调用接口
        openApi.remove(id).then((resp) => {
          //提示
          this.$message.success('删除成功!')
          //刷新页面
          this.getAll()
        })
      })
    },

    // 批量删除
    batchDelete() {
      if (this.ids.length > 0) {
        this.$confirm('此操作将永久删除实验室开放信息, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          // 调用接口
          openApi.batchRemove(this.ids).then((resp) => {
            // 提示
            this.$message.success('删除成功!')
            // 刷新页面
            this.getAll()
          })
        })
      } else {
        this.$message.warning('请至少选择一条数据')
      }
    },

    // 清空
    reset() {
      this.searchObj = {}
      this.getAll()
    }
  }
}
</script>

<style></style>
